import { Message } from 'element-ui'
import util from './util/common'
import cookieUtil from './util/cookie'
import baspUtilRequest from '../common/commonMethod'
let fromDisable = true// 表单可提交状态

export default {
    install (Vue) {
        // 1. 添加全局方法或属性
        /* Vue.myGlobalMethod = function() {
			// 逻辑...
			alert(1);
		}
		// 2. 添加全局资源
		Vue.directive('my-directive', {
			bind(el, binding, vnode, oldVnode) {
				// 逻辑...
			}
		})
		// 3. 注入组件
		Vue.mixin({
			created: function() {
				// 逻辑...
			}
		}) */
        /**
		 * @author：xmj
		 * @function: owner系统名称自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$ownerSysNameSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{2,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-system/system/queryByName', {
                        pageNum: 1,
                        pageSize: 10,
                        systemName: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            var obj = {
                                value: results[i].systemId + '/' + results[i].systemName,
                                label: results[i].systemId

                            }
                            results[i] = obj
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function: 子账号名自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$sysUsernameSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-user/sysAccount/selectBySysUsername', {
                        pageNum: 1,
                        pageSize: 10,
                        sysUserName: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            results[i].value = results[i].sysUserName
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function: 百源账号名自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$byUsernameSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{3,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-user/account/selectEmpByUsername', {
                        pageNum: 1,
                        pageSize: 10,
                        username: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            results[i].value = results[i].username
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function: 子系统编码自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$subsysCodeSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{2,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-system/module/queryByCode', {
                        pageNum: 1,
                        pageSize: 10,
                        moduleCode: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            var obj = {
                                value: results[i].moduleCode + '/' + results[i].moduleName,
                                label: results[i].moduleName
                            }
                            results[i] = obj
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function: 子系统编码自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$subsysNameSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{2,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-system/module/querySubsysByName  ', {
                        pageNum: 1,
                        pageSize: 10,
                        subsysName: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            var obj = {
                                code: results[i].moduleCode,
                                value: results[i].moduleName
                            }
                            results[i] = obj
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function:资源名称自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用	,系统所属资源
		 */
        Vue.prototype.$resourceNameSelectOwns = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{2,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-system/resource/selectOwnerRes', {
                        pageNum: 1,
                        pageSize: 10,
                        resourceName: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            var obj = {
                                value: results[i].resourceCode + '/' + results[i].resourceName,
                                label: results[i].resourceName,
                                id: results[i].resourceId
                            }
                            results[i] = obj
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function:资源名称自动补全
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$resourceNameSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{2,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-system/resource/selectByName', {
                        pageNum: 1,
                        pageSize: 10,
                        resourceName: queryString
                    }).then(function (response) {
                        const results = response.result.data
                        for (var i = 0; i < results.length; i++) {
                            var obj = {
                                value: results[i].resourceCode + '/' + results[i].resourceName,
                                label: results[i].resourceName,
                                id: results[i].resourceId
                            }
                            results[i] = obj
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }

        /**
		 * @author：ww
		 * @function: 工号模糊搜索下拉
		 * @pramas: el-autocomplete 必须基于这个控件使用
		 */
        Vue.prototype.$singlePersonSelect = function (queryString, cb) {
            var that = this
            if (/^[\u4e00-\u9fa5]/g.test(queryString) || /^\S{2,}$/.test(queryString)) {
                this.timeOut && clearTimeout(this.timeOut)
                this.timeOut = setTimeout(function () {
                    that.$httpExt().post('/dev-basp-user/account/selectByEmpCode', {
                        pageNum: 1,
                        pageSize: 10,
                        empCode: queryString
                    }).then(function (response) {
                        const results = response.result.list
                        for (var i = 0; i < results.length; i++) {
                            var obj = {
                                value: results[i].empCode
                            }
                            results[i] = obj
                        }
                        cb(results)
                    }, function (response) {
                        Message({
                            message: response.msg,
                            type: 'warning'
                        })
                    })
                }, 500)
            } else {
                cb([])
            }
        }
        /**
		 * @author：xmj
		 * @function: 防重
		 * @pramas: btn  可传可不传 传了btn置灰
		 */
        Vue.prototype.$checkPrevent = function (btn) {
            if (fromDisable) { // 验证表单状态是否为可以提交状态
                fromDisable = false // 点击后 把状态改为 不可提交
                setTimeout(function () {
                    fromDisable = true
                }, 3000) // 三秒之后把表单状态重新设置为可提交
                if (typeof btn !== 'undefined') { // 验证是否传入值
                    btn.style.background = '#ddd' // 把按钮颜色置灰
                    btn.style.color = '#fff' // 把按钮颜色置灰
                    btn.style.border = 0 // 把按钮颜色置灰
                    setTimeout(function () {
                        btn.style.background = ''
                        btn.style.color = ''
                        btn.style.border = ''
                    }, 3000) // 三秒后重新改为启用
                }
                return true // 如果状态为可提交 返回true
            } else {
                return false
            }
        }
        /**
		 * 剔除所有非数字的
		 */
        Vue.prototype.$getTextNumber = function (val) {
            val = val + ''
            var _str = val.replace(/(^\s*)|(\s*$)/g, "") // 去除字符串的左右两边空格
            _str = _str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "")// 去除表情符号
            return _str.replace(/[^\d.]/g, "")
        }
        /**
		 * 找第二个参数在第一个参数的索引值
		 */
        Vue.prototype.$findIndex = function (arr, obj, param) {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i][param] === obj[param]) {
                    return i
                }
            }
        }
        /**
		 * 剔除英文逗号和换行符
		 */
        Vue.prototype.$checkTextValidate = function (val) {
            var _str = val.replace(/(^\s*)|(\s*$)/g, "") // 去除字符串的左右两边空格
            _str = _str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "")// 去除表情符号
            return _str.replace(/[\r\n,]/g, "")
        }
        /**
		 * 查询系统信息
		 */
        Vue.prototype.$getSystemInfo = function () {
        	return new Promise((resolve) => {
        		var obj = sessionStorage.getItem('systemInfo')
	            if (obj) {
	            	resolve(JSON.parse(obj))
	            } else {
	                this.$httpExt().get('/dev-basp-user/account/getSysDetails')
	                .then((response) => {
	                    sessionStorage.setItem('systemInfo', JSON.stringify(response.result))
	                    resolve(response.result)
	                }, () => {
	                	resolve({})
	                })
	            }
		    })
	           
        }
        Vue.prototype.$setCookie = cookieUtil.setCookie
        Vue.prototype.$getCookie = cookieUtil.getCookie
        Vue.prototype.$delAllCookie = cookieUtil.delAllCookie
        Vue.prototype.$numberDigits = util.numberDigits
        Vue.prototype.$format = util.dateFormat
        Vue.prototype.$deepCopy = util.deepCopy
        Vue.prototype.$merge = util.merge
        Vue.prototype.$byAccountOptionsFun = baspUtilRequest.byAccountOptionsFun
        Vue.prototype.$getSubSystemList = baspUtilRequest.getSubSystemList
        Vue.prototype.$getroleListFun = baspUtilRequest.getroleListFun
        Vue.prototype.$resourceOptionsAllFun = baspUtilRequest.resourceOptionsAllFun
        Vue.prototype.$resourceOptionsFun = baspUtilRequest.resourceOptionsFun
        Vue.prototype.$dataGroupNameOptionsFun = baspUtilRequest.dataGroupNameOptionsFun
    }
}
